package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.LeaseFarmEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.FarmStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FarmStateModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private FarmStateFragment fragment;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;

    public FarmStateModel(FarmStateFragment fragment, SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //获取工厂上下架列表
    public void getPublishListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("pageNum", page);
        map.get().put("pageSize", size);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().getAllMyListFarm(map.get())
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseFarmEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseFarmEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseFarmEntity>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            fragment.showList(response.body().getData().getList(), isLoadMore);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseFarmEntity>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //上下架
    public void upDownStateModel(String opt, String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().offShelfFarm(id, opt)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            fragment.showOptSuccess(opt);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    //删除
    public void delModel(String opt, String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null);
        APIManager.getInstance().getLeaseAPI().deleteFarm(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            fragment.showOptSuccess(opt);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("farmDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                } else {
                    assert response.body() != null;
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
